package com.stone.business.plan.action;

/**
 * <pre>
 * ----计划列表 Action---
 * </pre>
 */

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpSession;

import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;

import com.stone.business.plan.pojo.TPlan;
import com.stone.business.plan.service.PlanService;
import com.stone.business.sbqxjl.pojo.TSbqxjl;
import com.stone.para.IConstant;
import com.stone.sys.pojo.TBdz;
import com.stone.sys.pojo.TDictionary;
import com.stone.sys.pojo.TOrgan;
import com.stone.sys.pojo.TUser;
import com.stone.utils.DateFormatTool;
import com.stone.utils.JsonUtils;
import com.stone.utils.StringUtils;

import frm.ssh.action.BaseListAction;
import frm.ssh.exception.AppException;

public class PlanListAction extends BaseListAction {

	private PlanService planService;
	
	/**
	 * @return the fromMenu
	 */
	public PlanService getPlanService() {
		return planService;
	}

	public void setPlanService(PlanService planService) {
		this.planService = planService;
		this.service=planService;
	}
	
	/** * 构造方法. */
    public PlanListAction() {
        this.entityClass = TPlan.class;
    }
	private String hiddenids;
	private List<TBdz> bdzList;
	private List<TDictionary> dydjList;
	private List<TDictionary> sblxList;
	private long plan_total;
	private long plan_prepared;
	private long plan_unpre;
	private long temp_sctotal;
	private long temp_scprepared;
	private long temp_scunpre;
	private long temp_gltotal;
	private long temp_glprepared;
	private long temp_glunpre;
	String month;
	private List list;
	String currentmonth;
	String ym;
	String monthid;
	HashMap<String, Object> m = new HashMap<String, Object>();
		public String getMonth() {
			return month;
		}

		public void setMonth(String month) {
			this.month = month;
		}

		public String getCurrentmonth() {
			return currentmonth;
		}

		public void setCurrentmonth(String currentmonth) {
			this.currentmonth = currentmonth;
		}

		public String getMonthid() {
			return monthid;
		}

		public void setMonthid(String monthid) {
			this.monthid = monthid;
		}
		//班组
		private List<TOrgan> bzList;
			public List<TOrgan> getBzList() {
			return bzList;
		}

		public void setBzList(List<TOrgan> bzList) {
			this.bzList = bzList;
		}

	/**
	 * 设置查询条件
	 */
	public HashMap<String, Object> setSearchCondition() {
		
		HashMap<String, Object> m = new HashMap<String, Object>();
		List<Object> valsList = new ArrayList<Object>();
		String organCode = String.valueOf(super.getSession().getAttribute(IConstant.SESSION_TORGAN_CODE));
		//m.put("cols", "plan");
		//m.put("cols", "new map(s.jhgzsjks as jhgzsjks, s.jhgzsjjs as jhgzsjjs,s.xkgzsj as xkgzsj, s.id as id,s.lx as lx,bdz.bdzname as bdzname, s.bdzname as bdzname, s.ph as ph, s.gzfzrname as gzfzrname, s.gzrw as gzrw,s.qxr as qxr)");
		m.put("cols", "new map(plan.id as id,plan.stationname as stationname,plan.responsible as responsible,plan.teamnames as teamnames,TO_CHAR(plan.startdate,'YYYY-MM-DD HH24:MI') as startdate,TO_CHAR(plan.enddate,'YYYY-MM-DD HH24:MI') as enddate,plan.powerrange as powerrange,plan.voltagelevel as voltagelevel,plan.workcontent as workcontent,plan.ysrname as ysrname,plan.dwryname as dwryname,plan.status as status)");
		String conditions = "";
		
		// 状态
		if ("0".equals(super.getSearchBean().getCon20())) {
			conditions = " status = '" + super.getSearchBean().getCon20() + "'";
		/*------------modify by fanghf 2012.3.15按状态过滤begin-----------*/
		} else if("1".equals(super.getSearchBean().getCon20())){
			conditions = " status = '1'";
		} else if("3".equals(super.getSearchBean().getCon20())||"4".equals(super.getSearchBean().getCon20())||"5".equals(super.getSearchBean().getCon20())){
			conditions = " (status = '9' or status = '1') ";//by wangtao update20120516 待审核计划也放到安排里
		} else {
			//conditions = " status != '0'";//by wangtao update20120426
			conditions = " 1=1 ";
		}
		
		/*------------modify by fanghf 2012.3.15按状态过滤end-----------*/
		Date sysDate = DateFormatTool.formatToDate(planService.getSysdate("yyyy-MM-dd"), false);
		Date grDate=DateFormatTool.backDayandWeek(sysDate, 2, false);
		Date drrDate=DateFormatTool.backDayandWeek(sysDate, 1, false);
		 String gr=DateFormatTool.format(grDate, false);
		 String drr=DateFormatTool.format(drrDate, false);
		 
		 //是否从个人待办事项进入计划列表 update by wangtao 2012-5-25
		 if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon19()) && ("grdbsx".equals(super.getSearchBean().getCon19()))){
			// System.out.println("======from menu======" + super.getSearchBean().getCon19());
			 conditions += " and to_char(SYSDATE+7, 'yyyy-mm-dd') BETWEEN to_char(startdate-3, 'yyyy-mm-dd') AND to_char(enddate+7, 'yyyy-mm-dd')";
		 }else{
		 
			// 隔日
			if ("3".equals(super.getSearchBean().getCon20())||"03".equals(super.getSearchBean().getCon5())) {
	
				//conditions += " and to_char(gr,'YYYYMMDD') = to_char(sysdate,'YYYYMMDD')";
	       // conditions += " and to_char(gr,'YYYYMMDD') = to_char(sysdate,'YYYYMMDD')";
	      //  conditions += " AND TO_CHAR(startdate,'yyyy-MM-dd') = '"+gr+"'";
				int n=DateFormatTool.dayOfWeek(sysDate, false);
				if(n==4||n==5){
					 conditions += " and '"+gr+"' BETWEEN to_char(startdate, 'yyyy-mm-dd') AND to_char(enddate+2, 'yyyy-mm-dd')";
						
				}else if(n==6){
					 conditions += " and '"+gr+"' BETWEEN to_char(startdate, 'yyyy-mm-dd') AND to_char(enddate+1, 'yyyy-mm-dd')";
						
				}else{
	        conditions += " and '"+gr+"' between to_char(plan.startdate,'YYYY-MM-DD') and to_char(plan.enddate,'YYYY-MM-DD')";
				}
			} else if ("4".equals(super.getSearchBean().getCon20())||"02".equals(super.getSearchBean().getCon5())) {
				//conditions += " AND TO_CHAR(startdate,'yyyy-MM-dd') = '"+drr+"'";
				//conditions += " and to_char(dyr,'YYYYMMDD') = to_char(sysdate,'YYYYMMDD')";
				conditions += " and '"+drr+"' between to_char(plan.startdate,'YYYY-MM-DD') and to_char(plan.enddate,'YYYY-MM-DD')";
			} else if ("5".equals(super.getSearchBean().getCon20())||"01".equals(super.getSearchBean().getCon5())) {
				//conditions += " and to_char(startdate,'YYYYMMDD') = to_char(sysdate,'YYYYMMDD')";
	
				//conditions += " and startdate - trunc(sysdate) = 0";
				conditions += " and to_char(sysdate,'YYYYMMDD') between to_char(plan.startdate,'YYYYMMDD') and to_char(plan.enddate,'YYYYMMDD')";
	
			}
		 }
		/*if ("3".equals(super.getSearchBean().getCon20())) {

			conditions += " and startdate - trunc(sysdate) = 2";

		} else if ("4".equals(super.getSearchBean().getCon20())) {

			conditions += " and startdate - trunc(sysdate) = 1";

		} else if ("5".equals(super.getSearchBean().getCon20())) {

			conditions += " and startdate - trunc(sysdate) = 0";

		}*/
		// conditions += " and s.lx = bdz.id  "; 
			
		TOrgan o  = (TOrgan)super.getSession().getAttribute(IConstant.SESSION_TORGAN);
		TUser loginUser  = (TUser)super.getSession().getAttribute(IConstant.SESSION_TUSER);//系统当前登录用户
		/*if (o.getOrganCode().toString().length() > 6 && !"1".equals(super.getSession().getAttribute("SESSION_ISADMIN"))) {
			conditions += " and ouid = '" + o.getOrganCode() + "'";
		}*/
		if (!StringUtils.isNullOrBlank(super.getSearchBean().getCon11())) {
			conditions += " and to_char(startdate,'YYYY-MM') = '" + super.getSearchBean().getCon11() + "'";
		}
		//----------------------查询条件---------------------
		/*if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon1())){
			conditions += " AND plan.ouid like ? ";
			valsList.add(organCode+ "%");
		}*/
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon19()) && "grdbsx".equals(super.getSearchBean().getCon19())){//登录用户是工作负责人或班组成员
			//conditions += " AND plan.fzr = '"+loginUser.getId()+ "'" ;
			//conditions += " AND (plan.fzr = ?  or plan.fzr in (select t.cyid from TPlanBz t where t.planid= plan.id))" ;
			//valsList.add(("'"+loginUser.getId()+ "'").toString());
			conditions += " AND (plan.responsible = '"+loginUser.getName()+ "'  or plan.id in (select t.planid from TPlanBz t where t.planid= plan.id and delflag='0' and t.cyname like '%"+loginUser.getName()+ "%'))" ;
			
		}
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon19()) && "bzdbsx".equals(super.getSearchBean().getCon19())){//登录用户是配合班组且计划班组负责人或成员为空
			conditions += " AND (plan.phbz = '"+o.getId()+ "'  and plan.id in (select t.planid from TPlanBz t where t.planid= plan.id and t.bzid= plan.phbz and (t.cyname is null or t.fzrname is null)))" ;
			
		}
		// 开始时间
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon2())){
			conditions += " AND TO_CHAR(plan.startdate,'yyyy-MM-dd') >= ?";
			valsList.add(super.getSearchBean().getCon2());
		}
		
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon3())){
			conditions += " AND TO_CHAR(plan.startdate,'yyyy-MM-dd') <= ?";
			valsList.add(super.getSearchBean().getCon3());
		}
		
		/*// 班组
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon5())){
			conditions += " AND plan.teamnames like ? ";
			valsList.add("%" + super.getSearchBean().getCon5().replaceAll(",", "%") + "%");
		}*/
		// 班组
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon18())) {
			conditions += " and plan.id in (select t.planid from TPlanBz t where t.planid= plan.id and t.delflag=0 and t.bzid = '"+super.getSearchBean().getCon18()+"')";
			
		}
		// 变电站 by wangtao add20121121
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon7())) {
			conditions += " AND plan.stationid in (";
			StringBuffer buf= new StringBuffer(""); 
			String[] bdznames = ((String) super.getSearchBean().getCon8() == null? "" : super.getSearchBean().getCon8()).split(",");
			
			for (int i=0; i< bdznames.length; i++) { 
			  if (i!=0) buf.append(", "); 
			  buf.append("" + bdznames[i]+ ""); 
			 
			} 
			buf.append(")"); 
			conditions +=buf;

			
		}
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon6())){
			if(!super.getSearchBean().getCon6().equals("1")){
			conditions += " AND plan.plantype = ?";
			valsList.add(super.getSearchBean().getCon6());
			}
		}
		if(loginUser==null){//检修网浏览
			HttpSession s = super.getRequest().getSession(false);
			Enumeration enm = super.getRequest().getSession(false).getAttributeNames();
			Object attribute = null;
			
			/*// 清除SESSION
			while (enm.hasMoreElements()) {
				attribute = enm.nextElement();

				if (!attribute.equals(IConstant.SESSION_TORGAN)
					&& !attribute.equals(IConstant.SESSION_TORGAN_CODE)
					&& !attribute.equals(IConstant.SESSION_TUSER)
					&& !attribute.equals(IConstant.SESSION_ISADMIN)){

					try {
						s.removeAttribute((String)attribute);
					} catch (Exception e) {
						
					}
				}
			}*/
			
			List<TUser> list = planService.findListByHql("from TUser where loginId = 'guest'") ;
			
			TUser user = list.get(0);
					
			// 是否管理员（1是,0不是）
			this.setSessionAttribute(IConstant.SESSION_ISADMIN, user.getIsadmin());
			// 用户信息放入SESSION
			this.setSessionAttribute(IConstant.SESSION_TUSER, user);
			String roles = "";
			// 角色传放入SESSION
			this.setSessionAttribute(IConstant.SESSION_ROLES, roles);
			// 机构信息
			TOrgan organ = (TOrgan)this.planService.findById(user.getOrgan().getId(), TOrgan.class.getName());
			this.setSessionAttribute(IConstant.SESSION_TORGAN, organ);
			this.setSessionAttribute("btn_query_display", "1");
		}
		m.put("conditions",conditions);
		m.put("clause"," order by plan.startdate asc");
		m.put("alias", "plan");//	m.put("alias", "s, TBdz bdz");
		m.put("vals", valsList.toArray());
		
		setDictionary();
		
		return m;
		
	}

	
	// 获得字典项目
	private void setDictionary() {
		
		// 变电站
		this.bdzList = planService.findListByHql(" from TBdz order by sortid");
		// 电压等级
		this.dydjList = planService.findListByHql(" from TDictionary where itemType = 'DYDJ' order by sortid");
		// 电压等级
		this.sblxList = planService.findListByHql(" from TDictionary where itemType = 'SBLX' order by sortid");
		//班组
	 	String bzsql = "from TOrgan where id <=24 and id >=21 order by id";
	 	bzList = planService.findListByHql(bzsql);
	}

	//工作准备 
	public String workPlan(){
//		String sql1 = "";
//		String sql2 = "";
//		String condition1 = " where status = '9'";
//		String condition2 = " where status = '9'";
//		sql1 = "select a.id,A.TEAMNAMES,A.STARTDATE,A.ENDDATE,A.POWERRANGE,A.VOLTAGELEVEL,A.WORKCONTENT,A.STATUS,case when 1=1 then '1' end as datafrom," +
//			   " case when c.id is null then '0' else '1' end as ishavegzd" +
//			   " from tab_plan a left join tab_gzrwd c" +
//			   " on c.planid = a.id";
//		
//		if ("3".equals(super.getSearchBean().getCon20())) {
//			condition1 += " and a.startdate - trunc(sysdate) = 2";
//		}else if("4".equals(super.getSearchBean().getCon20())){
//			condition1 += " and a.startdate - trunc(sysdate) = 1";
//		}else if("5".equals(super.getSearchBean().getCon20())){
//			condition1 += " and a.startdate - trunc(sysdate) = 0";
//		}
//		
//		TOrgan o  = (TOrgan)super.getSession().getAttribute(IConstant.SESSION_TORGAN);
//		if (o.getOrganCode().toString().length() > 6 && !"1".equals(super.getSession().getAttribute("SESSION_ISADMIN"))) {
//			condition1 += " and a.ouid = '" + o.getOrganCode() + "'";
//		}
//		sql1 += condition1 ;
//		
//		sql2 ="select b.id,B.BZNAME as teamnames,B.STARTDATE,B.ENDDATE,B.TDFW as POWERRANGE,B.DYDJ as VOLTAGELEVEL,B.GZXM as WORKCONTENT,B.STATUS,case when 1=1 then '0' end as datafrom,"
//			+ " case when d.id is null then '0' else '1' end as ishavegzd"
//			+ " from tab_lsap b left join tab_gzrwd d"
//			+ " on d.planid = b.id";
//		
//		if ("3".equals(super.getSearchBean().getCon20())) {
//			condition2 += " and b.startdate - trunc(sysdate) = 2";
//		}else if("4".equals(super.getSearchBean().getCon20())){
//			condition2 += " and b.startdate - trunc(sysdate) = 1";
//		}else if("5".equals(super.getSearchBean().getCon20())){
//			condition2 += " and b.startdate - trunc(sysdate) = 0";
//		}
//		if (o.getOrganCode().toString().length() > 6 && !"1".equals(super.getSession().getAttribute("SESSION_ISADMIN"))) {
//			condition2 += " and b.ouid = '" + o.getOrganCode() + "'";
//		}
//		sql2 += condition2;
//		
//		this.grgzapList = planService.findListBySql(sql1 + " union " + sql2);
		String sql = "";
		String conditions = " (a.status = '9' or a.status = '1') ";//by wangtao update20120516 待审核计划也放到安排里
		Date sysDate = DateFormatTool.formatToDate(planService.getSysdate("yyyy-MM-dd"), false);
		Date grDate=DateFormatTool.backDayandWeek(sysDate, 2, false);
		Date drrDate=DateFormatTool.backDayandWeek(sysDate, 1, false);
		 String gr=DateFormatTool.format(grDate, false);
		 String drr=DateFormatTool.format(drrDate, false);
		if ("3".equals(super.getSearchBean().getCon20())) {
			//conditions += " AND TO_CHAR(a.startdate,'yyyy-MM-dd') = '"+gr+"'";
			//conditions += " and a.startdate - trunc(sysdate) = 2";
			//conditions += " and '"+gr+"' between to_char(a.startdate,'YYYY-MM-DD') and to_char(a.enddate,'YYYY-MM-DD')";
			int n=DateFormatTool.dayOfWeek(sysDate, false);
			if(n==4||n==5){
				 conditions += " and '"+gr+"' BETWEEN to_char(startdate, 'yyyy-mm-dd') AND to_char(enddate+2, 'yyyy-mm-dd')";
					
			}else if(n==6){
				 conditions += " and '"+gr+"' BETWEEN to_char(startdate, 'yyyy-mm-dd') AND to_char(enddate+1, 'yyyy-mm-dd')";
					
			}else{
        conditions += " and '"+gr+"' between to_char(a.startdate,'YYYY-MM-DD') and to_char(a.enddate,'YYYY-MM-DD')";
			}
		} else if ("4".equals(super.getSearchBean().getCon20())) {
			//conditions += " and a.startdate - trunc(sysdate) = 1";
			//conditions += " AND TO_CHAR(a.startdate,'yyyy-MM-dd') = '"+drr+"'";
			conditions += " and '"+drr+"' between to_char(a.startdate,'YYYY-MM-DD') and to_char(a.enddate,'YYYY-MM-DD')";
		} else if ("5".equals(super.getSearchBean().getCon20())) {
			//conditions += " and a.startdate - trunc(sysdate) = 0";
			//conditions += " and to_char(startdate,'YYYYMMDD') = to_char(sysdate,'YYYYMMDD')";
			conditions += " and to_char(sysdate,'YYYYMMDD') between to_char(a.startdate,'YYYYMMDD') and to_char(a.enddate,'YYYYMMDD')";

		}
		
		TOrgan o  = (TOrgan)super.getSession().getAttribute(IConstant.SESSION_TORGAN);
		/*if (o.getOrganCode().toString().length() > 6 && !"1".equals(super.getSession().getAttribute("SESSION_ISADMIN"))) {
			conditions += " and a.ouid = '" + o.getOrganCode() + "'";
		}*/
		//查询总数暂时这样，待修改
		//月度工作计划总数
		//sql = "select * from tab_plan a left join tab_plan_bz b on a.id=b.planid where b.delflag='0' and a.plantype='1' and " + conditions;
		sql = "select * from tab_plan a where a.plantype='1' and " + conditions;
		plan_total = getCount(sql);
		
		//月度工作计划已准备条数 update20120517 该计划有工单就算已准备,不管有几个工单
		//sql = "select * from tab_plan a,tab_gzrwd b where a.plantype='1' and b.planid = a.id and " + conditions;
		sql = "select distinct(b.planid) from tab_plan a,tab_gzrwd b where a.plantype='1' and b.planid = a.id and " + conditions;
		plan_prepared = getCount(sql);
		
		//月度工作计划未准备条数
		plan_unpre = plan_total - plan_prepared;
		
		//临时工作安排生产计划总数
		//sql = "select * from tab_lsap a where state = '0' and " + conditions;
		//sql = "select * from tab_plan a left join tab_plan_bz b on a.id=b.planid where a.plantype='2' and " + conditions;
		sql = "select * from tab_plan a where a.plantype='2' and " + conditions;
		temp_sctotal = getCount(sql);
		
		//临时工作安排生产计划已准备条数
		//sql = "select * from tab_lsap a,tab_gzrwd b where b.planid = a.id and a.state = '0' and " + conditions;
		//sql = "select * from tab_plan a,tab_gzrwd b where a.plantype='2' and b.planid = a.id and " + conditions;
		sql = "select distinct(b.planid) from tab_plan a,tab_gzrwd b where a.plantype='2' and b.planid = a.id and " + conditions;
		temp_scprepared = getCount(sql);
		
		//临时工作安排生产计划未准备条数
		temp_scunpre = temp_sctotal - temp_scprepared;
		
		//临时工作安排管理工作总数
		sql = "select * from tab_lsap a where state = '1' and " + conditions;
		temp_gltotal = getCount(sql);
		
		//临时工作安排管理工作已准备条数
		sql = "select * from tab_lsap a,tab_gzrwd b where b.planid = a.id and a.state = '1' and " + conditions;
		temp_glprepared = getCount(sql);
		
		//临时工作安排管理工作未准备条数
		temp_glunpre = temp_gltotal - temp_glprepared;
		
		return "WORKPLAN";
	}
	
	private int getCount(String sql){
		int count = 0;
		List templist = null;
		templist = planService.findListBySql(sql);
		if(templist != null){
			count = templist.size();
		}
		return count;
	}
	/**
	 * 获得左侧树
	 */
	public String toMonthTree() {
		
		List<HashMap<String, Object>> l = planService.findListBySql("select id as monthid, to_char(startdate, 'YYYY-MM') as currentmonth from tab_plan where TO_CHAR(startdate, 'yyyy-MM') = TO_CHAR(sysdate, 'yyyy-MM')  ");
		if(l.size()!=0){
		currentmonth = l.get(0).get("currentmonth").toString();
		monthid = l.get(0).get("monthid").toString();
		}
		return "TREE_SUCCESS";
	}
public void getMonthDate() throws Exception {
		
		List<HashMap<String,Object>> listTree;
		
		// 左侧的树
		if ("-1".equals(month)) {
			
			listTree = planService.findListBySql("select to_char(startdate,'YYYY') as text, to_char(startdate,'YYYY') as id, 0 as leaf from tab_plan group by to_char(startdate,'YYYY') order by id");

		// 维护时根节点
		} else if (month.length() == 4) {
			listTree = planService.findListBySql("select to_char(startdate,'MM') as text, '" + month + "' || '-' || to_char(startdate,'MM') as id, 1 as leaf  from tab_plan where to_char(startdate,'YYYY') = '" + month + "' group by to_char(startdate,'MM') order by id");
		} else {
			listTree = planService.findListBySql("select to_char(startdate,'MM') as text, '" + month + "' || '-' || to_char(startdate,'MM') as id, 1 as leaf  from tab_plan where to_char(startdate,'YYYY') = '" + month + "' group by to_char(startdate,'MM') order by id");
			//listTree = myService.findListBySql("select to_char(planweekstart,'DD') || '~' || to_char(planweekend,'DD') as text, id as id, 1 as leaf from tab_plan_week where to_char(planweekstart,'YYYY-MM') = '" + month + "' order by id");
		}
		/*else {
			listTree = myService.findListBySql("select to_char(startdate,'MM') as text, '" + month + "' || '-' || to_char(startdate,'MM') as id, 1 as leaf  from tab_plan_month where to_char(startdate,'YYYY') = '" + month + "' group by to_char(startdate,'MM') order by id");
		}	*/
        JsonConfig jsonConfig = JsonUtils.configJson(new String[] {}, "yyyy-MM-dd");
    	
        JSON json = JSONSerializer.toJSON(listTree, jsonConfig);
        
        super.getResponse().getWriter().print(json.toString());
	}
/**
 * 进入详细页面
 * @return
 * @throws AppException
 */
public String initDetail() throws AppException {
	
	//getRole();

	// 修改进入
	if (!StringUtils.isNullOrBlank(ym)) {
		list = planService.findListBySql(" select * from TAB_PLAN where to_char(startdate,'YYYY-MM') = '" + ym + "' order by startdate ");

		// 新建进入
	} else {

		list = new ArrayList();

	}

	return SUCCESS;
}
	public List<TBdz> getBdzList() {
		return bdzList;
	}

	public List<TDictionary> getDydjList() {
		return dydjList;
	}

	public List<TDictionary> getSblxList() {
		return sblxList;
	}
	
	public String getHiddenids() {
		return hiddenids;
	}

	public void setHiddenids(String hiddenids) {
		this.hiddenids = hiddenids;
	}

	public long getPlan_total() {
		return plan_total;
	}

	public void setPlan_total(long planTotal) {
		plan_total = planTotal;
	}

	public long getPlan_prepared() {
		return plan_prepared;
	}

	public void setPlan_prepared(long planPrepared) {
		plan_prepared = planPrepared;
	}

	public long getPlan_unpre() {
		return plan_unpre;
	}

	public void setPlan_unpre(long planUnpre) {
		plan_unpre = planUnpre;
	}

	public long getTemp_sctotal() {
		return temp_sctotal;
	}

	public void setTemp_sctotal(long tempSctotal) {
		temp_sctotal = tempSctotal;
	}

	public long getTemp_scprepared() {
		return temp_scprepared;
	}

	public void setTemp_scprepared(long tempScprepared) {
		temp_scprepared = tempScprepared;
	}

	public long getTemp_scunpre() {
		return temp_scunpre;
	}

	public void setTemp_scunpre(long tempScunpre) {
		temp_scunpre = tempScunpre;
	}

	public long getTemp_gltotal() {
		return temp_gltotal;
	}

	public void setTemp_gltotal(long tempGltotal) {
		temp_gltotal = tempGltotal;
	}

	public long getTemp_glprepared() {
		return temp_glprepared;
	}

	public void setTemp_glprepared(long tempGlprepared) {
		temp_glprepared = tempGlprepared;
	}

	public long getTemp_glunpre() {
		return temp_glunpre;
	}

	public void setTemp_glunpre(long tempGlunpre) {
		temp_glunpre = tempGlunpre;
	}

	public void setBdzList(List<TBdz> bdzList) {
		this.bdzList = bdzList;
	}

	public void setDydjList(List<TDictionary> dydjList) {
		this.dydjList = dydjList;
	}

	public void setSblxList(List<TDictionary> sblxList) {
		this.sblxList = sblxList;
	}

	

	
	public void setList(List list) {
		this.list = list;
	}

	public String getYm() {
		return ym;
	}

	public void setYm(String ym) {
		this.ym = ym;
	}
	/**
	 * 生成csv
	 */
	@Override
	public HashMap<String, Object> createCsvMap() {
		m=this.setSearchCondition();
		m.put("title", "设备停电计划");
		m.put("searchcols", "serialno,startdate,powerrange,gdqy,workcontent,jhgztime,jhgztime,yxfs,sqdw,remarks");
		m.put("colscn", "序号,日期,停电范围 ,影响供电区域,工作项目,计划工作时间,计划停电时间 ,运行方式安排,申请单位,备注");
		m.put("colsen", new String[] {"serialno","startdate","powerrange","gdqy","workcontent","jhgztime","jhgztime","yxfs","sqdw","remarks"});
		m.put("classname", TPlan.class.getName() + " s, TDictionary dic");
		
		return m;
	}
////////////////////////by wangtao add20121128
	 public String createSqlCvs()
    throws Exception
{
    HashMap m = createCsvMap();
   // List l = service.getListByDHql((String)m.get("searchcols"), (String)m.get("conditions"), (Object[])m.get("vals"), (String)m.get("clause"), (String)m.get("classname"));
    String sql="select plan.id as id,plan.stationname||':'||plan.powerrange as powerrange,CASE WHEN TO_CHAR(plan.startdate, 'YYYY-MM-DD') = TO_CHAR(plan.enddate, 'YYYY-MM-DD') THEN TO_CHAR(plan.startdate, 'YYYY-MM-DD') WHEN TO_CHAR(plan.startdate, 'YYYY-MM-DD') < TO_CHAR(plan.enddate, 'YYYY-MM-DD') THEN TO_CHAR(plan.startdate, 'YYYY-MM-DD') || '--' ||TO_CHAR(plan.enddate, 'MM-DD') end as startdate,CASE WHEN TO_CHAR(plan.startdate,'YYYY-MM-DD')=TO_CHAR(plan.enddate,'YYYY-MM-DD') THEN TO_CHAR(plan.startdate,'HH24:MI')||'--'||TO_CHAR(plan.enddate,'HH24:MI') WHEN TO_CHAR(plan.startdate, 'YYYY-MM-DD') < TO_CHAR(plan.enddate, 'YYYY-MM-DD') THEN TO_CHAR(plan.startdate, 'MM-DD HH24:MI') || '--' ||TO_CHAR(plan.enddate, 'MM-DD HH24:MI') end as jhgztime,TO_CHAR(plan.enddate,'YYYY-MM-DD HH24:MI') as enddate,plan.workcontent as workcontent,plan.remarks as remarks from tab_plan plan where plan.powerrange is not null and ";
   // String conditions=(String)m.get("conditions");
    String conditions = "";
	
	// 状态
	if ("0".equals(super.getSearchBean().getCon20())) {
		conditions = " status = '" + super.getSearchBean().getCon20() + "'";
	/*------------modify by fanghf 2012.3.15按状态过滤begin-----------*/
	} else if("1".equals(super.getSearchBean().getCon20())){
		conditions = " status = '1'";
	} else if("3".equals(super.getSearchBean().getCon20())||"4".equals(super.getSearchBean().getCon20())||"5".equals(super.getSearchBean().getCon20())){
		conditions = " (status = '9' or status = '1') ";//by wangtao update20120516 待审核计划也放到安排里
	} else {
		//conditions = " status != '0'";//by wangtao update20120426
		conditions = " 1=1 ";
	}
	
	/*------------modify by fanghf 2012.3.15按状态过滤end-----------*/
	Date sysDate = DateFormatTool.formatToDate(planService.getSysdate("yyyy-MM-dd"), false);
	Date grDate=DateFormatTool.backDayandWeek(sysDate, 2, false);
	Date drrDate=DateFormatTool.backDayandWeek(sysDate, 1, false);
	 String gr=DateFormatTool.format(grDate, false);
	 String drr=DateFormatTool.format(drrDate, false);
	 
	 //是否从个人待办事项进入计划列表 update by wangtao 2012-5-25
	 if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon19()) && ("grdbsx".equals(super.getSearchBean().getCon19()))){
		// System.out.println("======from menu======" + super.getSearchBean().getCon19());
		 conditions += " and to_char(SYSDATE+7, 'yyyy-mm-dd') BETWEEN to_char(startdate-3, 'yyyy-mm-dd') AND to_char(enddate+7, 'yyyy-mm-dd')";
	 }else{
	 
		// 隔日
		if ("3".equals(super.getSearchBean().getCon20())||"03".equals(super.getSearchBean().getCon5())) {

			int n=DateFormatTool.dayOfWeek(sysDate, false);
			if(n==4||n==5){
				 conditions += " and '"+gr+"' BETWEEN to_char(startdate, 'yyyy-mm-dd') AND to_char(enddate+2, 'yyyy-mm-dd')";
					
			}else if(n==6){
				 conditions += " and '"+gr+"' BETWEEN to_char(startdate, 'yyyy-mm-dd') AND to_char(enddate+1, 'yyyy-mm-dd')";
					
			}else{
        conditions += " and '"+gr+"' between to_char(plan.startdate,'YYYY-MM-DD') and to_char(plan.enddate,'YYYY-MM-DD')";
			}
		} else if ("4".equals(super.getSearchBean().getCon20())||"02".equals(super.getSearchBean().getCon5())) {
			//conditions += " AND TO_CHAR(startdate,'yyyy-MM-dd') = '"+drr+"'";
			//conditions += " and to_char(dyr,'YYYYMMDD') = to_char(sysdate,'YYYYMMDD')";
			conditions += " and '"+drr+"' between to_char(plan.startdate,'YYYY-MM-DD') and to_char(plan.enddate,'YYYY-MM-DD')";
		} else if ("5".equals(super.getSearchBean().getCon20())||"01".equals(super.getSearchBean().getCon5())) {
			//conditions += " and to_char(startdate,'YYYYMMDD') = to_char(sysdate,'YYYYMMDD')";

			//conditions += " and startdate - trunc(sysdate) = 0";
			conditions += " and to_char(sysdate,'YYYYMMDD') between to_char(plan.startdate,'YYYYMMDD') and to_char(plan.enddate,'YYYYMMDD')";

		}
	 }		
	TOrgan o  = (TOrgan)super.getSession().getAttribute(IConstant.SESSION_TORGAN);
	TUser loginUser  = (TUser)super.getSession().getAttribute(IConstant.SESSION_TUSER);//系统当前登录用户
   if (!StringUtils.isNullOrBlank(super.getSearchBean().getCon11())) {
		conditions += " and to_char(startdate,'YYYY-MM') = '" + super.getSearchBean().getCon11() + "'";
	}
	//----------------------查询条件---------------------
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon19()) && "grdbsx".equals(super.getSearchBean().getCon19())){//登录用户是工作负责人或班组成员
		conditions += " AND (plan.responsible = '"+loginUser.getName()+ "'  or plan.id in (select t.planid from tab_plan_bz t where t.planid= plan.id and delflag='0' and t.cyname like '%"+loginUser.getName()+ "%'))" ;
		
	}
	if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon19()) && "bzdbsx".equals(super.getSearchBean().getCon19())){//登录用户是配合班组且计划班组负责人或成员为空
		conditions += " AND (plan.phbz = '"+o.getId()+ "'  and plan.id in (select t.planid from tab_plan_bz t where t.planid= plan.id and t.bzid= plan.phbz and (t.cyname is null or t.fzrname is null)))" ;
		
	}
	// 开始时间
	if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon2())){
		conditions += " AND TO_CHAR(plan.startdate,'yyyy-MM-dd') >= '" + super.getSearchBean().getCon2() + "'";
		
	}
	
	if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon3())){
		conditions += " AND TO_CHAR(plan.startdate,'yyyy-MM-dd') <= '" + super.getSearchBean().getCon3() + "'";
		
	}
	
	// 班组
	if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon18())) {
		conditions += " and plan.id in (select t.planid from tab_plan_bz t where t.planid= plan.id and t.delflag=0 and t.bzid = '"+super.getSearchBean().getCon18()+"')";
		
	}
	// 变电站 by wangtao add20121121
	if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon7())) {
		conditions += " AND plan.stationid in (";
		StringBuffer buf= new StringBuffer(""); 
		String[] bdznames = ((String) super.getSearchBean().getCon8() == null? "" : super.getSearchBean().getCon8()).split(",");
		
		for (int i=0; i< bdznames.length; i++) { 
		  if (i!=0) buf.append(", "); 
		  buf.append("" + bdznames[i]+ ""); 
		 
		} 
		buf.append(")"); 
		conditions +=buf;

		
	}
	if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon6())){
		if(!super.getSearchBean().getCon6().equals("1")){
		conditions += " AND plan.plantype = '" + super.getSearchBean().getCon6() + "'";
		
		}
	}
    sql=sql+conditions+" order by plan.startdate";
    List l = service.findListBySql(sql);
    StringBuffer text = new StringBuffer();
    String title = (String)m.get("title");
    text.append((new StringBuilder(String.valueOf(title))).append("\r\n").toString());
    text.append((new StringBuilder(String.valueOf((String)m.get("colscn")))).append("\r\n").toString());
    String col[] = (String[])m.get("colsen");
    for(int i = 0; i < l.size(); i++)
    {
        HashMap map = (HashMap)l.get(i);
        for(int j = 0; j < col.length; j++)
        {
            text.append("\"");
            if(map.get(col[j]) == null&&j==0)
                text.append(i+1);
            else 
            if(map.get(col[j]) == null&&j!=0)
                text.append("");
            else
            if(map.get(col[j]) instanceof Timestamp)
                text.append(DateFormatTool.format((Date)map.get(col[j]), true));
            else
            if(map.get(col[j]) instanceof String)
                text.append(String.valueOf(map.get(col[j])));
            else
            if(map.get(col[j]) instanceof Double)
                text.append(map.get(col[j]) != null && ((Double)map.get(col[j])).doubleValue() != 0.0D ? formatterDouble.format(map.get(col[j])) : "0.00");
            else
            if(map.get(col[j]) instanceof Long)
                text.append(map.get(col[j]) != null && ((Long)map.get(col[j])).longValue() != 0L ? formatterInteger.format(map.get(col[j])) : "0");
            else
            if(map.get(col[j]) instanceof Integer)
                text.append(map.get(col[j]) != null && ((Integer)map.get(col[j])).intValue() != 0 ? formatterInteger.format(map.get(col[j])) : "0");
            else
                text.append(map.get(col[j]));
            text.append("\",");
        }

        text.append("\r\n");
    }

    returnStream = new ByteArrayInputStream(text.toString().getBytes());
    return "CSV_SUCCESS";
}
	///////////////////////////////////////////////////////////
	 private InputStream returnStream;
	    private String csvFileName;
	    static DecimalFormat formatterDouble = new DecimalFormat("#,###,###,###,###.##");
	    static DecimalFormat formatterInteger = new DecimalFormat("#,###,###,###,###");
	    public InputStream getReturnStream()
	    {
	        return returnStream;
	    }
}
